package com.loyal.loyalerp.humenresource.role.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.loyal.loyalerp.humenresource.role.mapper.GyRoleMapper;
import com.loyal.loyalerp.humenresource.role.model.GyRole;
import com.loyal.loyalerp.humenresource.role.service.RoleService;
import com.loyal.loyalerp.utils.ValidateUtil;



@Service
public class RoleServiceImpl implements RoleService{
	@Autowired
	GyRoleMapper gyRoleMapper;
	
	final static String tableName = "gy_role";
//	@Override
//	public List<GyRole> alllistRole() {
//		// TODO Auto-generated method stub
//		return gyRoleMapper.alllistRole();
//	}
	
	@Override
	public PageInfo<GyRole> listRole(Integer PageNum, Integer recordNum, String sord) {
		// TODO Auto-generated method stub
		Page<GyRole> page=PageHelper.startPage(PageNum, recordNum);
		gyRoleMapper.listRole(sord);
		PageInfo<GyRole> pageInfo = new PageInfo<>(page);
		
		
		return pageInfo;
	}

	
	@Override
	public PageInfo<GyRole> findRole(Integer PageNum,Integer recordNum, String sord,String searchField, String searchString,String searchOper) {
		// TODO Auto-generated method stub
		if(searchField.equals("companyname")) {
			searchField="companyid";
			Integer id=gyRoleMapper.findCompanyId(searchString);
			searchString=Integer.toString(id);
		}
		Page<GyRole> page=PageHelper.startPage(PageNum, recordNum);
		gyRoleMapper.findRole(searchField, searchString,searchOper);

		PageInfo<GyRole> pageInfo = new PageInfo<>(page);
		
		return pageInfo;
		
	}


	@Override
	public String editRole(GyRole gyRole,String companyname,String oper) {
		// TODO Auto-generated method stub
		if(oper.equals("add")) {
			
			if (!ValidateUtil.validateAdd(tableName)) {
				return "no permission";
			}
			
			gyRole.setCompanyid(gyRoleMapper.companyListByName(companyname));
			try {
				gyRoleMapper.insertSelective(gyRole);
				return "success";
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
				throw e;
			}
		}
		if(oper.equals(new String("edit"))) {
			
			if (!ValidateUtil.validateEdit(tableName)) {
				return "no permission";
			}
			
			gyRole.setCompanyid(gyRoleMapper.companyListByName(companyname));
			try {
				gyRoleMapper.updateByPrimaryKeySelective(gyRole);
				return "success";
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
				throw e;
			}
		}
		if(oper.equals(new String("del"))) {
			
			if (!ValidateUtil.validateDel(tableName)) {
				return "no permission";
			}
			
			try {
				gyRoleMapper.deleteByPrimaryKey(gyRole.getId());
				return "success";
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
				throw e;
			}
		}
		return "fail";
	}



}
